#!/bin/sh
# - tool
# - tool_arguments

. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/wait.sh
. $LKP_SRC/lib/run-env.sh

: "${tool:=$1}"
[ -z "$tool" ] && die "No tool specified"

setup_wait

local_run || {
	linux_headers_dir=$(ls -d /usr/src/linux-headers*-bpf)
	[ -z "$linux_headers_dir" ] && die "failed to find linux-headers package"
	build_link="/lib/modules/$(uname -r)/build"
	ln -sf "$linux_headers_dir" "$build_link"
}

tools_dirs="/usr/share/bcc/tools/ /lkp/$LKP_USER/download/"

for td in $tools_dirs; do
	[ -f "$td/$tool" ] && tool_file="$td/$tool"
	[ -f "$td/${tool}.py" ] && tool_file="$td/${tool}.py"
done

[ -n "$tool_file" ] || die "No such tool: $tool"

tmpdir=$(mktemp -d bpfcc-tools-XXXXXX)

cd "$tmpdir" || die "Failed to goto temporary directory"

cp "$tool_file" "$tool.py"

cat > run-bpf.py <<EOF
import signal

def on_int(*args):
    raise KeyboardInterrupt()

signal.signal(signal.SIGINT, on_int)

import $tool
EOF

echo "tool: $tool"

ARCH=x86 python run-bpf.py $tool_arguments &

pid=$!

wait_post_test

kill -INT "$pid"
